package session

import (
	"OnlineRetailers/models/Admin"
	"crypto/rand"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io"
)

func GenerationSession() (sessionid string, err error) {

	var session Admin.Session
	b := make([]byte, 16)
	if _, err = io.ReadFull(rand.Reader, b); err != nil {
		return
	}

	for {
		session.SessionId = base64.URLEncoding.EncodeToString(b)
		exit, err := ExistSession(session.SessionId)
		fmt.Println(exit, err)
		if !exit {
			break
		}
	}
	sessionJson, err := json.Marshal(session)
	if err != nil {
		return
	}
	err = NewSession(&session, sessionJson)
	if err != nil {
		return
	}

	sessionid = session.SessionId
	return

}
